//
//  MKUILayoutConstraintItemAttribute.m
//  MKUI
//
//  Created by 苏尚进 on 2023/2/19.
//  Copyright © 2023 moon. All rights reserved.
//

#import "MKUILayoutConstraintItemAttribute.h"
#import "MKUILayoutConstraint.h"

@implementation MKUILayoutConstraintItemAttributeBase
- (CGSize)size{
    return self.layoutFrame.size;
}
- (void)setSize:(CGSize)size{
    CGRect f = self.layoutFrame;
    f.size = size;
    self.layoutFrame = f;
}
- (CGPoint)origin{
    return self.layoutFrame.origin;
}
- (void)setOrigin:(CGPoint)origin{
    CGRect f = self.layoutFrame;
    f.origin = origin;
    self.layoutFrame = f;
}
@end

@implementation MKUILayoutConstraintItemAttribute
- (id)initWithItem:(id<MKUILayoutConstraintItemAttributeProtocol>)item{
    if(self=[self init]){
        self.item = item;
    }
    return self;
}
- (void)applyAttribute{
    self.item.layoutFrame = self.layoutFrame;
}
- (void)applyAttributeWithResizeItems:(BOOL)resizeItems{
    if([self.item isKindOfClass:[MKUILayoutConstraint class]]){
        MKUILayoutConstraint *item = (MKUILayoutConstraint *)self.item;
        [item setBounds:self.layoutFrame];
    }else{
        self.item.layoutFrame = self.layoutFrame;
    }
    if([self.item conformsToProtocol:@protocol(MKUILayoutConstraintItemProtocol)]){
        id<MKUILayoutConstraintItemProtocol> item = (id<MKUILayoutConstraintItemProtocol>)self.item;
        if([item respondsToSelector:@selector(layoutItemsWithResizeItems:)]){
            [item layoutItemsWithResizeItems:resizeItems];
        }
    }
}
@end

@interface MKUILayoutConstraintItemAttributeSection(){
    NSMutableArray<id<MKUILayoutConstraintItemAttributeProtocol>> *_allItemAttributes;
}
@end

@implementation MKUILayoutConstraintItemAttributeSection
- (id)init{
    if(self=[super init]){
        _allItemAttributes = [[NSMutableArray alloc] init];
    }
    return self;
}
- (void)addItemAttribute:(id<MKUILayoutConstraintItemAttributeProtocol>)itemAttribute{
    [_allItemAttributes addObject:itemAttribute];
}
- (void)insertItemAttribute:(id<MKUILayoutConstraintItemAttributeProtocol>)itemAttribute atIndex:(NSInteger)index{
    [_allItemAttributes insertObject:itemAttribute atIndex:index];
}
- (void)removeItemAttributeAtIndex:(NSInteger)index{
    [_allItemAttributes removeObjectAtIndex:index];
}
- (void)setItemAttributs:(NSArray<id<MKUILayoutConstraintItemAttributeProtocol>> *)itemAttributes{
    [_allItemAttributes removeAllObjects];
    [_allItemAttributes addObjectsFromArray:itemAttributes];
}
- (NSArray<id<MKUILayoutConstraintItemAttributeProtocol>> *)itemAttributs{
    return _allItemAttributes;
}
- (CGSize)sizeThatFlowLayoutItemsWithSpacing:(CGFloat)itemSpacing axis:(MKCGAxis)X{
    CGSize size = CGSizeZero;
    CGFloat sumWidth = 0;
    CGFloat maxHeight = 0;
    int count = 0;
    CGFloat w = 0;
    CGFloat h = 0;
    CGRect f = CGRectZero;
    MKCGAxis Y = MKCGAxisReverse(X);
    for(id<MKUILayoutConstraintItemAttributeProtocol> attr in _allItemAttributes){
        f = attr.layoutFrame;
        w = MKCGRectGetLength(f, X);
        h = MKCGRectGetLength(f, Y);
        if(w>0 && h>0){
            count++;
            sumWidth += w;
            maxHeight = MAX(maxHeight,h);
        }
    }
    if(count>0){
        sumWidth += itemSpacing*(count-1);
        MKCGSizeSetLength(&size, X, sumWidth);
        MKCGSizeSetLength(&size, Y, maxHeight);
    }
    return size;
}
- (void)flowLayoutItemsWithSpacing:(CGFloat)itemSpacing axis:(MKCGAxis)X alignment:(MKCGRectAlignment)alignY needRevert:(BOOL)needRevert{
    MKCGAxis Y = MKCGAxisReverse(X);
    CGRect bounds = self.layoutFrame;
    CGRect f1 = CGRectZero;
    f1.origin = bounds.origin;
    for(id<MKUILayoutConstraintItemAttributeProtocol> item in needRevert?_allItemAttributes.reverseObjectEnumerator:_allItemAttributes){
        f1.size = item.layoutFrame.size;
        MKCGRectAlignToRect(&f1, Y, alignY, bounds);
        item.layoutFrame = f1;
        if(MKCGRectGetLength(f1, X)>0 && MKCGRectGetLength(f1, Y)>0){
            MKCGRectSetMin(&f1, X, MKCGRectGetMax(f1, X)+itemSpacing);
        }
    }
}
@end
